package com.web.kongjian.tag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.mysteel.util.DateUtils;
import cn.mysteel.util.ObjectUtils;
import cn.mysteel.util.StringUtils;

import com.base.app.entity.Article;
import com.base.app.entity.ArticlePicture;
import com.base.app.service.ArticlePictureService;
import com.base.app.service.ArticleService;
import com.base.forum.entity.Invitation;
import com.base.forum.service.InvitationService;
import com.base.kongjian.entity.Comment;
import com.base.kongjian.entity.Kongjian;
import com.base.kongjian.entity.PopularitySystem;
import com.base.kongjian.entity.RealInform;
import com.base.kongjian.entity.Reply;
import com.base.kongjian.entity.WealthSystem;
import com.base.kongjian.entity.Weibo;
import com.base.kongjian.entity.WorksContributeRec;
import com.base.kongjian.entity.WorksNumCount;
import com.base.kongjian.service.CommentService;
import com.base.kongjian.service.KongjianService;
import com.base.kongjian.service.PopularitySystemService;
import com.base.kongjian.service.RealInformService;
import com.base.kongjian.service.ReplyService;
import com.base.kongjian.service.WealthSystemService;
import com.base.kongjian.service.WeiboService;
import com.base.kongjian.service.WorksContributeRecService;
import com.base.kongjian.service.WorksNumCountService;
import com.base.user.entity.Member;
import com.base.user.service.MemberService;
import com.mysteel.core.SpringContext;
import com.mysteel.page.Page;
import com.mysteel.page.PageService;
import com.web.kongjian.dto.ComNumWeiboDTO;
import com.web.kongjian.dto.Comm_ReplyDTO;
import com.web.kongjian.dto.HotWeiboDTO;
import com.web.kongjian.dto.WorksContributeDTO;

public class IndexTag
{
	
	private WorksContributeRecService worksContributeRecService;
	private WeiboService weiboService;
	private MemberService memberService;
	private RealInformService realInformService;
	private CommentService commentService;
	private ReplyService replyService;
	private KongjianService kongjianService;
	private PopularitySystemService popularitySystemService;
	private WealthSystemService wealthSystemService;
	private WorksNumCountService worksNumCountService;
	private PageService pageService;
	private ArticleService articleService;
	private ArticlePictureService articlePictureService;
	private InvitationService invitationService;
	
	public IndexTag ()
	{
		worksContributeRecService = (WorksContributeRecService) SpringContext.getBean ("worksContributeRecService");
		weiboService = (WeiboService) SpringContext.getBean ("weiboService");
		memberService = (MemberService) SpringContext.getBean ("memberService");
		realInformService = (RealInformService) SpringContext.getBean ("realInformService");
		commentService = (CommentService) SpringContext.getBean ("commentService");
		replyService = (ReplyService) SpringContext.getBean ("replyService");
		kongjianService = (KongjianService) SpringContext.getBean ("kongjianService");
		popularitySystemService = (PopularitySystemService) SpringContext.getBean ("popularitySystemService");
		wealthSystemService = (WealthSystemService) SpringContext.getBean ("wealthSystemService");
		worksNumCountService = (WorksNumCountService) SpringContext.getBean ("worksNumCountService");
		pageService = (PageService) SpringContext.getBean ("pageService");
		articleService = (ArticleService) SpringContext.getBean ("articleService");
		articlePictureService = (ArticlePictureService) SpringContext.getBean ("articleServicePicture");
		invitationService = (InvitationService) SpringContext.getBean ("invitationService");
	}
	
	/**
	 * ����΢��
	 * @param request
	 * @return
	 */
	public List<HotWeiboDTO> hotWeiboTag()
	{
		List<HotWeiboDTO> returnList = null;
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put ("workKind" ,1);
		Page<WorksContributeRec> page = worksContributeRecService.queryWCR (params ,0 ,-1);
		
		if(ObjectUtils.notNull (page))
		{
			List<WorksContributeRec> list = page.getDatas ();
			list = removeSaveWeibo (list);
			List<WorksContributeDTO> allContributes = new ArrayList<WorksContributeDTO>();
			for(WorksContributeRec wc:list)
			{
				Map<String, Object> pa = new HashMap<String,Object>();
				pa.put ("workId" ,wc.getWorkId ());
				pa.put ("workKind" ,1);
				
				Page<WorksContributeRec> pages = worksContributeRecService.queryWCR (params ,0 ,-1);
				
				if(ObjectUtils.notNull (pages))
				{
					int allContributerValue = 0;
					
					for(WorksContributeRec ws:pages.getDatas ())
					{
						allContributerValue = allContributerValue+ws.getContributeVal ();//�õ�ĳ��΢�����ܵĹ���ֵ
					}
					
					WorksContributeDTO wcDto = new WorksContributeDTO ();
					wcDto.setWorkId (wc.getWorkId ());
					wcDto.setAllContributeValue (allContributerValue);
					allContributes.add (wcDto);
				}
			}
			allContributes = weiboSortByContributeValue(allContributes);
			
			returnList = new ArrayList<HotWeiboDTO>();
			
			
			int size = allContributes.size ();
			
			int length = 11;
			
			if(size<11)
			{
				length = size;
			}
			
			for(int i = 0;i<length;i++)
			{
				WorksContributeDTO wdL = allContributes.get (i);
				HotWeiboDTO hwDto = new HotWeiboDTO ();
				
				Weibo weibo = weiboService.getById (wdL.getWorkId ());
				
				if(ObjectUtils.notNull (weibo))
				{
					long memberId = weibo.getMemberId ();
					
					Member m = memberService.getMemberById (memberId);
					
					if(ObjectUtils.notNull (m))
					{
						hwDto.setAnthorId (m.getId ());
						hwDto.setAuthor (m.getNickName ());
						hwDto.setId (wdL.getWorkId ());
						hwDto.setTitle (weibo.getTitle ());
						hwDto.setTime (DateUtils.formatDate (weibo.getPublishTime () ,"yyyy-MM-dd"));
						
						returnList.add (hwDto);
					}
				}
			}
		}
		
		return returnList;
	}
	
	/**
	 * ȥ��
	 * @param list
	 * @return
	 */
	public List<WorksContributeRec> removeSaveWeibo(List<WorksContributeRec> list)
	{
		long length = list.size ();
		
		for(int i = 0;i<length;i++)
		{
			for(int j = i+1;j<list.size();j++)
			{
				if(ObjectUtils.notNull (list.get (i))&&ObjectUtils.notNull (list.get (j)))
				{
					if(list.get (i).getWorkId ()==list.get (j).getWorkId ())
					{
						list.remove (j);
					}
				}
			}
			
		}
		
		return list;
	}
	
	/**
	 * ����΢��������ֵ��С����
	 * @param list
	 * @return
	 */
	public List<WorksContributeDTO> weiboSortByContributeValue(List<WorksContributeDTO> list)
	{
		for(int i = 0;i<list.size ();i++)
		{
			for(int j = 1;j<list.size ();j++)
			{
				if(list.get (j-1).getAllContributeValue  ()<list.get (j).getAllContributeValue ())
				{
					WorksContributeDTO temp = null;
					temp = list.get (j-1);
					list.set (j-1 ,list.get (j));
					list.set (j ,temp);
				}
			}
		}
		
		return list;
	}
	
	
	/**
	 * �Ƽ���΢����
	 * @return
	 */
	public List<Member> recommend()
	{
		List<Member> returnList = null;
		
		Page<RealInform> page = realInformService.queryRealInform (null ,1 ,6);
		
		if(ObjectUtils.notNull (page))
		{
			returnList = new ArrayList<Member>();
			
			for(RealInform r:page.getDatas ())
			{
				Member m = memberService.getMemberById (r.getMemberId ());
				
				if(ObjectUtils.notNull (m))
				{
					String headUrl = m.getHeadUrl ();
					
					if(StringUtils.isTrimEmpty (headUrl))
					{
						headUrl = "/skin/images/kongjian/noavatar_small.gif";
						m.setHeadUrl (headUrl);
					}
					
					returnList.add (m);
				}
			}
		}
		
		return returnList ;
	}
	
	
	
	/**
	 * 
	 * @return
	 */
	public List<ComNumWeiboDTO> comNumWeiboTag(int maxCount)
	{
		List<ComNumWeiboDTO> returnList = null;
		Page<Weibo> page = weiboService.queryWeibo (null ,0 ,-1);
		
		if(ObjectUtils.notNull (page))
		{
			List<Weibo> list = page.getDatas ();
			
			if(ObjectUtils.notEmpty (list))
			{
				
				list = weiboSortByCommentNum(list);
				int size = list.size ();
				int length = maxCount;
				if(size<maxCount)
				{
					length = size;
				}
				returnList = new ArrayList<ComNumWeiboDTO>();
				for(int i = 0;i<length;i++)
				{
					Weibo weibo = list.get (i);
					if(ObjectUtils.notNull (weibo))
					{
						Member member = memberService.getMemberById (weibo.getMemberId ());
						
						if(ObjectUtils.notNull (member))
						{
							ComNumWeiboDTO cwbdto = new ComNumWeiboDTO ();
							cwbdto.setAuthor (member.getNickName ());
							cwbdto.setClickNum (weibo.getBrowseCount ());
							cwbdto.setCommentNum (weibo.getCommentCount ());
							cwbdto.setId (weibo.getId ());
							cwbdto.setLength (weibo.getContent ().length ());
							cwbdto.setTime (DateUtils.formatDate (weibo.getPublishTime () ,"yyyy-MM-dd"));
							cwbdto.setTitle (weibo.getTitle ());
							
							Map<String, Object> params = new HashMap<String,Object>();
							
							params.put ("workKind" ,1);
							params.put ("workId" ,weibo.getId ());
							
							Page<Comment> pageC = commentService.queryComment (params ,0 ,-1);
							
							if(ObjectUtils.notNull (pageC))
							{
								
								List<Comment> comments = pageC.getDatas ();
								
								if(ObjectUtils.notEmpty (comments))
								{
									Comment lastComment = comments.get (comments.size ()-1);//������һ������
									
									if(ObjectUtils.notNull (lastComment))
									{
										Map<String, Object> paramsR = new HashMap<String,Object>();
										
										paramsR.put ("commentId" ,lastComment.getId ());
										
										Page<Reply> pageR = replyService.queryReply (paramsR ,0 ,-1);
										
										if(ObjectUtils.notNull (pageR))
										{
											List<Reply> replies = pageR.getDatas ();
											
											if(ObjectUtils.notEmpty  (replies))
											{
												Reply lastReply = replies .get (replies.size()-1);//������һ���ظ�
												
												if(ObjectUtils.notNull (lastReply))
												{
													long sendM_Id = lastReply.getReply_MId ();
													
													Member lastRM = memberService.getMemberById (sendM_Id);//������ظ��Ļ�Ա
													if(ObjectUtils.notNull (lastRM))
													{
														cwbdto.setLastReplyAuthor (lastRM.getNickName ());
														cwbdto.setLastReplyTime (DateUtils.formatDate (lastReply.getReplyTime () ,"yyyy-MM-dd"));
														
														returnList.add (cwbdto);
													}
												}
											}
										}else
										{
											//��ĳ���������޻ظ����򷵻����һ������
											long sendM_Id = lastComment.getComment_MId  ();
											
											Member lastRM = memberService.getMemberById (sendM_Id);//������ظ��Ļ�Ա
											if(ObjectUtils.notNull (lastRM))
											{
												cwbdto.setLastReplyAuthor (lastRM.getNickName ());
												cwbdto.setLastReplyTime (DateUtils.formatDate (lastComment.getCommentTime  () ,"yyyy-MM-dd"));
												
												returnList.add (cwbdto);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		
		return returnList;
	}
	
	/**
	 * ����΢��������ֵ��С����
	 * @param list
	 * @return
	 */
	public List<Weibo> weiboSortByCommentNum(List<Weibo> list)
	{
		for(int i = 0;i<list.size ();i++)
		{
			for(int j = 1;j<list.size ();j++)
			{
				if(list.get (j-1).getCommentCount   ()<list.get (j).getCommentCount ())
				{
					Weibo temp = null;
					temp = list.get (j-1);
					list.set (j-1 ,list.get (j));
					list.set (j ,temp);
				}
			}
		}
		
		return list;
	}
	
	/**
	 * ��ϸ
	 * @return
	 */
	public Map<String, Object> detailTag(HttpServletRequest request)
	{
		Map<String, Object> returnMap = new HashMap<String,Object>();
		long id = Long.parseLong (request.getParameter ("id"));
		int kind = Integer.parseInt (request.getParameter ("kind"));
		long memberId = 0L;
		if(kind == 1)
		{
			Weibo weibo = weiboService.getById (id);
			returnMap.put ("weibo" ,weibo);
			if(ObjectUtils.notNull (weibo))
			{
				memberId = weibo.getMemberId ();
			}
		}
		if(kind==4)
		{
			Invitation weibo = invitationService.getById (id) ;
			returnMap.put ("weibo" ,weibo);
			if(ObjectUtils.notNull (weibo))
			{
				memberId = weibo.getPublisherId  ();
			}
		}
		
		Member m = memberService.getMemberById (memberId);
		returnMap.put ("member" ,m);
		
		Kongjian kongjian = kongjianService.getKJByMemberId (memberId);
		returnMap.put ("kongjian" ,kongjian);
		PopularitySystem pSystem = popularitySystemService.getByMemberId (memberId);
		returnMap.put ("popularitySystem" ,pSystem);
		WealthSystem ws = wealthSystemService.getWSByMemberId (memberId);
		returnMap.put ("wealthSystem" ,ws);
		WorksNumCount  wc = worksNumCountService.getItsByMemberIed (memberId);
		returnMap.put ("worksNumCount" ,wc);
		
		return returnMap;
	}
	
	
	/**
	 * ��ϸҳ���ۺͻظ�
	 * @param request
	 * @param MaxPageRowCount
	 * @return
	 */
	public Page<Map<String,Object>> comm_replyDetailTag(HttpServletRequest request,int MaxPageRowCount)
	{
		Page<Map<String,Object>> returnPage = null;
		long id = Long.parseLong (request.getParameter ("id"));
		int kind = Integer.parseInt (request.getParameter ("kind"));
		String pageString = request.getParameter ("current_page");
		String forumIdStr = request.getParameter ("forumId");
		long current_page = 1;
		if(!StringUtils.isTrimEmpty (pageString))
		{
			current_page = Long.parseLong (pageString);
		}
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("workId" ,id);
		params.put ("workKind" ,kind);
		
		List<Comm_ReplyDTO> listComm_Re = null;
		List<Map<String, Object>> dateList = null;
		
		Page<Comment> comPage = commentService.queryComment (params ,0 ,-1);
		
		if(ObjectUtils.notNull (comPage))
		{
			listComm_Re = new ArrayList<Comm_ReplyDTO> ();
			List<Comment> list_c = comPage.getDatas ();
			
			for(Comment c:list_c)
			{
				Comm_ReplyDTO cDto = new Comm_ReplyDTO ();
				cDto.setContent (c.getContent ());
				cDto.setCreateTime (c.getCommentTime ());
				cDto.setMemberId (c.getMemberId ());
				cDto.setReply_MId (c.getMemberId ());
				if(kind!=4)
				{
					listComm_Re.add (cDto);
				}
				
				Map<String, Object> pa = new HashMap<String,Object>();
				pa.put ("commentId" ,c.getId ());
				
				Page<Reply> rePage = replyService.queryReply (pa ,0 ,-1);
				
				if(ObjectUtils.notNull (rePage))
				{
					for(Reply r:rePage.getDatas ())
					{
						cDto = new Comm_ReplyDTO ();
						cDto.setContent (r.getContent ());
						cDto.setCreateTime (r.getReplyTime ());
						cDto.setMemberId (r.getReply_MId ());
						cDto.setReply_MId (r.getComment_MId ());
						listComm_Re.add (cDto);
					}
				}
			}
			
			listComm_Re = com_ReplySortByCreateTime(listComm_Re);
			
			long size = listComm_Re.size ();
			long remainder = size%MaxPageRowCount;
			long TotalRowCount = 0L;
			
			if(remainder!=0)
			{
				TotalRowCount = (size/MaxPageRowCount)+1;
			}else
			{
				TotalRowCount = size/MaxPageRowCount;
			}
			
			List<Comm_ReplyDTO> newCom_Repls = new ArrayList<Comm_ReplyDTO>();
			
			if(current_page==TotalRowCount&&ObjectUtils.notEmpty (listComm_Re))
			{
				for(int i = (int)current_page*MaxPageRowCount-2;i<size;i++)
				{
					newCom_Repls.add (listComm_Re.get (i));
				}
			}else if(current_page==1&&ObjectUtils.notEmpty (listComm_Re))
			{
				for(int i = 0;i<MaxPageRowCount;i++)
				{
					newCom_Repls.add (listComm_Re.get (i));
				}
			}
			else if(ObjectUtils.notEmpty (listComm_Re))
			{
				for(int i = (int)(current_page*MaxPageRowCount-2);i<(current_page*MaxPageRowCount-2)+MaxPageRowCount;i++)
				{
					newCom_Repls.add (listComm_Re.get (i));
				}
			}
			
			if(ObjectUtils.notNull (newCom_Repls))
			{
				dateList = new ArrayList<Map<String, Object>>();
				
				for(int  i = 0;i<newCom_Repls.size ();i++)
				{
					Comm_ReplyDTO cReplyDTO = newCom_Repls.get (i);
					dateList.add (com_ReplyReturnMap(cReplyDTO,i,(int)current_page,(int)MaxPageRowCount));
				}
				returnPage = new Page<Map<String,Object>> ();
				returnPage.setDatas (dateList);
				returnPage.setTotalRowCount ((int)size);
				returnPage.setCurrentPageNum ((int)current_page);
				returnPage.setTotalPageNum ((int)TotalRowCount);
				returnPage.setMaxPageRowCount (MaxPageRowCount);
				if(StringUtils.isTrimEmpty (forumIdStr))
				{
					returnPage.setURL ("?&current_page=${pageNum}&kind="+kind+"&id="+id);
				}else
				{
					returnPage.setURL ("?&current_page=${pageNum}&kind="+kind+"&id="+id+"&forumId="+forumIdStr);
				}
				returnPage.setTemplate ("pshow");
				pageService.build (returnPage);
			}
		}
		
		return returnPage;
	}
	
	/**
	 * ����createTime����
	 * @param list
	 * @return
	 */
	public List<Comm_ReplyDTO> com_ReplySortByCreateTime(List<Comm_ReplyDTO> list)
	{
		for(int i = 0;i<list.size ();i++)
		{
			for(int j = 1;j<list.size ();j++)
			{
				if(list.get (j-1).getCreateTime  ()<list.get (j).getCreateTime ())
				{
					Comm_ReplyDTO temp = null;
					temp = list.get (j-1);
					list.set (j-1 ,list.get (j));
					list.set (j ,temp);
				}
			}
		}
		
		return list;
	}
	
	
	public Map<String, Object> com_ReplyReturnMap(Comm_ReplyDTO c,int i,int currentPage,int max)
	{
		Map<String, Object> returnMap = new HashMap<String,Object>();
		long memberId = c.getMemberId ();
		long reply_MId = c.getReply_MId ();
		
		Member replyMember = memberService.getMemberById (reply_MId);
		returnMap.put ("replyMember" ,replyMember);
		
		returnMap.put ("content" ,c.getContent ());
		returnMap.put ("createTime" ,c.getCreateTime ());
		
		Member m = memberService.getMemberById (memberId);
		returnMap.put ("member" ,m);
		returnMap.put ("L" ,i+(currentPage*max)-1);
		
		Kongjian kongjian = kongjianService.getKJByMemberId (memberId);
		returnMap.put ("kongjian" ,kongjian);
		PopularitySystem pSystem = popularitySystemService.getByMemberId (memberId);
		returnMap.put ("popularitySystem" ,pSystem);
		WealthSystem ws = wealthSystemService.getWSByMemberId (memberId);
		returnMap.put ("wealthSystem" ,ws);
		WorksNumCount  wc = worksNumCountService.getItsByMemberIed (memberId);
		returnMap.put ("worksNumCount" ,wc);
		
		return returnMap;
	}
	
	/**
	 * �ռ���Ѷ
	 * @param channelId
	 * @param request
	 * @param maxPageRowCount
	 * @return
	 */
	public List<Article > kongjianNewsTag(long channelId,HttpServletRequest request,int maxPageRowCount,int currentPage)
	{
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("channelId" ,channelId);
		
		Page<Article> page = articleService.queryArticle (params ,currentPage ,maxPageRowCount);
		
		if(ObjectUtils.notNull (page))
		{
			
			return page.getDatas ();
		}
		
		return null;
	}
	
	
	/**
	 * ������ϸ
	 * @param request
	 * @return
	 */
	public Article articleDetailTag(HttpServletRequest request)
	{
		long id = Long.parseLong (request.getParameter ("ar_Id"));
		
		return articleService.getArticleById (id);
	}
	
	
	/**
	 * ͨ������ID���ͼƬurl
	 * @param articleId
	 * @return
	 */
	public String getArticlePicUrl(long articleId)
	{
		Map<String, Object> params = new HashMap<String,Object> ();
		
		params.put ("articleId" ,articleId);
		
		Page<ArticlePicture> page = articlePictureService.queryArticlePicture (params ,0 ,-1);
		
		if(ObjectUtils.notNull (page))
		{
			for(ArticlePicture a:page.getDatas ())
			{
				return a.getUrl ();
			}
		}
		
		return null;
	}
}
